<template>
  <div
    @click="contextmenuFlag=false"
    class="avue-tags">
    <!-- tag盒子 -->
    <div
      v-if="contextmenuFlag"
      :style="{left:contentmenuX+'px',top:contentmenuY+'px'}"
      class="avue-tags__contentmenu">
      <div
        class="item"
        @click="closeOthersTags">关闭其他
      </div>
      <div
        class="item"
        @click="closeAllTags">关闭全部
      </div>
    </div>
    <div
      :class="{'avue-tags__box--close':!website.isFirstPage}"
      class="avue-tags__box">
      <div class="top-bar__left">
        <div
          v-if="showCollapse"
          :class="[{ 'avue-breadcrumb--active': isCollapse }]"
          class="avue-breadcrumb">
          <i
            class="icon-navicon"
            @click="setCollapse"/>
        </div>
      </div>
      <el-tabs
        v-model="active"
        :closable="tagLen!==1"
        type="card"
        @contextmenu.native="handleContextmenu"
        @tab-click="openTag"
        @edit="menuTag">
        <el-tab-pane
          v-for="item in tagList"
          :key="item.value"
          :label="item.title || item.label"
          :name="item.value"/>

      </el-tabs>
      <div class="top-bar__right">
        <el-tooltip
          v-if="showColor"
          effect="dark"
          content="主题色"
          placement="bottom">
          <div class="top-bar__item">
            <top-color/>
          </div>
        </el-tooltip>
        <el-tooltip
          :content="
            unreadMessage == 0 ? '没有未读消息' : `${unreadMessage}条未读消息`
          "
          effect="dark"
          placement="bottom"
        >
          <div class="top-bar__item">
            <top-news />
          </div>
        </el-tooltip>
        <el-tooltip
          v-if="showDebug"
          :content="logsFlag?'没有错误日志':`${logsLen}条错误日志`"
          effect="dark"
          placement="bottom">
          <div class="top-bar__item">
            <top-logs/>
          </div>
        </el-tooltip>
        <el-tooltip
          v-if="showLock"
          effect="dark"
          content="锁屏"
          placement="bottom">
          <div class="top-bar__item">
            <top-lock/>
          </div>
        </el-tooltip>
        <el-tooltip
          v-if="showTheme"
          effect="dark"
          content="特色主题"
          placement="bottom">
          <div class="top-bar__item top-bar__item--show">
            <top-theme/>
          </div>
        </el-tooltip>
        <el-tooltip
          v-if="showFullScreen"
          :content="isFullScreen?'退出全屏':'全屏'"
          effect="dark"
          placement="bottom">
          <div class="top-bar__item">
            <i
              :class="isFullScreen?'icon-zuixiaohua':'icon-quanpingzuidahua'"
              @click="handleScreen"/>
          </div>
        </el-tooltip>
        <el-tooltip
          v-if="userInfo.avatar"
          effect="dark"
          content="用户头像"
          placement="bottom">
          <img
            id="thumbnail"
            class="top-bar__img">
        </el-tooltip>
        <el-dropdown>
          <span class="el-dropdown-link">
            {{ userInfo.username }}
            <i class="el-icon-arrow-down el-icon--right"/>
          </span>
          <el-dropdown-menu slot="dropdown">
            <!-- <el-dropdown-item divided>
              <router-link to="/">首页</router-link>
            </el-dropdown-item> -->
            <el-dropdown-item divided>
              <router-link to="/info/index">个人信息</router-link>
            </el-dropdown-item>
            <el-dropdown-item
              divided
              @click.native="$refs.seting.open()">界面设置
            </el-dropdown-item>
            <el-dropdown-item
              divided
              @click.native="logout">退出系统
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <top-setting ref="seting"/>
      </div>
      <!-- <el-dropdown class="avue-tags__menu">
        <el-button
          type="primary"
          size="mini">
          更多
          <i class="el-icon-arrow-down el-icon--right"/>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="closeOthersTags">关闭其他</el-dropdown-item>
          <el-dropdown-item @click.native="closeAllTags">关闭全部</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown> -->
    </div>

  </div>
</template>
<script>
  import { mapGetters, mapState } from 'vuex'
  import { fullscreenToggel, handleImg, listenfullscreen } from '@/util/util'
  import topLock from './top/top-lock'
  import topMenu from './top/top-menu'
  import topTheme from './top/top-theme'
  import topLogs from './top/top-logs'
  import topColor from './top/top-color'
  import topSetting from './top/top-setting'
  import topNews from "./top/top-news";

  export default {
    name: 'Tags',
    data() {
      return {
        active: '',
        contentmenuX: '',
        contentmenuY: '',
        contextmenuFlag: false
      }
    },
    components: {
      topLock,
      topMenu,
      topTheme,
      topLogs,
      topColor,
      topNews,
      topSetting
    },
    watch: {
      tag() {
        this.setActive()
      },
      contextmenuFlag() {
        window.addEventListener('mousedown', this.watchContextmenu)
      }
    },
    created() {
      handleImg(this.userInfo.avatar, 'thumbnail')
    },
    mounted() {
      this.setActive()
      listenfullscreen(this.setScreen)
    },
    computed: {
      ...mapGetters([
        'tagWel',
        'tagList',
        'tag',
        'website',
        'userInfo',
        'isFullScreen',
        'isCollapse',
        'logsLen',
        "unreadMessage",
        'logsFlag'
      ]),
      ...mapState({
        showDebug: state => state.common.showDebug,
        showTheme: state => state.common.showTheme,
        showLock: state => state.common.showLock,
        showFullScreen: state => state.common.showFullScreen,
        showCollapse: state => state.common.showCollapse,
        showMenu: state => state.common.showMenu,
        showColor: state => state.common.showColor,
        showTag: state => state.common.showTag
      }),
      tagLen() {
        return this.tagList.length || 0
      }
    },
    methods: {
      watchContextmenu() {
        if (!this.$el.contains(event.target) || event.button !== 0) {
          this.contextmenuFlag = false
        }

        window.removeEventListener('mousedown', this.watchContextmenu)
      },
      handleScreen() {
        fullscreenToggel()
      },
      setCollapse() {
        this.$store.commit('SET_COLLAPSE')
      },
      setScreen() {
        this.$store.commit('SET_FULLSCREEN')
      },
      logout() {
        this.$confirm('是否退出系统, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('LogOut').then(() => {
              this.$router.push({ path: '/login' })
              // window.location.reload()
          })
        })
      },
      handleContextmenu(event) {
        let target = event.target
        // 解决 https://github.com/d2-projects/d2-admin/issues/54
        let flag = false
        if (target.className.indexOf('el-tabs__item') > -1) flag = true
        else if (target.parentNode.className.indexOf('el-tabs__item') > -1) {
          target = target.parentNode
          flag = true
        }
        if (flag) {
          event.preventDefault()
          event.stopPropagation()
          this.contentmenuX = event.clientX
          this.contentmenuY = event.clientY
          this.tagName = target.getAttribute('aria-controls').slice(5)
          this.contextmenuFlag = true
        }
      },
      // 激活当前选项
      setActive() {
        this.active = this.tag.value
      },
      menuTag(value, action) {
        if (action === 'remove') {
          let { tag, key } = this.findTag(value)
          this.$store.commit('DEL_TAG', tag)
          if (tag.value === this.tag.value) {
            tag = this.tagList[key === 0 ? key : key - 1] // 如果关闭本标签让前推一个
            this.openTag(tag)
          }
        }
      },
      openTag(item) {
        let tag
        if (item.name) {
          tag = this.findTag(item.name).tag
        } else {
          tag = item
        }
        this.$router.push({
          path: this.$router.$avueRouter.getPath({
            name: tag.label,
            src: tag.value
          }),
          meta: {
            title: item.label
          },
          query: tag.query
        }).catch(() => {})
      },
      closeOthersTags() {
        this.contextmenuFlag = false
        this.$store.commit('DEL_TAG_OTHER')
      },
      findTag(value) {
        let tag, key
        this.tagList.map((item, index) => {
          if (item.value === value) {
            tag = item
            key = index
          }
        })
        return { tag: tag, key: key }
      },
      closeAllTags() {
        this.contextmenuFlag = false
        this.$store.commit('DEL_ALL_TAG')
        this.$router.push({
          path: this.$router.$avueRouter.getPath({
            src: this.tagWel.value
          }),
          query: this.tagWel.query
        })
      }
    }
  }
</script>
<style scoped lang="scss">
.el-dropdown-link{
  cursor: pointer;
  padding: 10px 0;
}
#thumbnail{
  cursor: pointer;
}
</style>

